import { getCitiesApi, getCountriesApi } from "@/api/country"
import { getOrganizationApi } from "@/api/departmentalOrganization";
import { getJobsApi } from "@/api/job";
import { getOrganizationTypesApi } from "@/api/organization";
import { getAllPositionsApi } from "@/api/position";
import { getRanksApi } from "@/api/rank";
import { getNewestWorkNumberDataApi } from "@/api/staff";
import { getWorkplacesApi } from "@/api/workplace";
import { useState } from "react";

interface rowsData {
    rows: [],
    total: number
}


const useFindData = () => {

    const [countriesData, setCountriesData] = useState<{ _id: string, name: string }[]>([]);
    const [citiesData, setCitiesData] = useState([]);
    const [organizationTypesData, setOrganizationTypesData] = useState({ rows: [], total: 0 });
    const [jobsData, setJobsData] = useState<rowsData>({ rows: [], total: 0 });
    const [positionsData, setPositionsData] = useState<rowsData>({ rows: [], total: 0 });
    const [ranksData, setRanksData] = useState<rowsData>({ rows: [], total: 0 });
    const [workplaceData, setWorkPlaceData] = useState<rowsData>({ rows: [], total: 0 });
    const [organizationData, setOrganizationData] = useState<rowsData>({ rows: [], total: 0 });
    const [workNumberData, setWorkNumberData] = useState<any>({});
    //获取国家
    const getCountriesHook = () => {
        getCountriesApi().then(res => {
            if (res.code == 200) {
                setCountriesData(res.data)
            }
        })
    }
    //获取城市
    const getCitiesHook = (data: string) => {
        getCitiesApi({ name: data }).then(res => {
            if (res.code == 200) {
                setCitiesData(res.data)
            }
        })
    }
    //获取组织类型
    const getOrganizationTypesHook = (data?: object) => {
        getOrganizationTypesApi(data).then(res => {
            if (res.code == 200) {
                setOrganizationTypesData(res.data)
            }
        })
    }
    //获取职务
    const getJobsHook = (data?: object) => {
        getJobsApi(data).then(res => {
            if (res.code == 200) {
                setJobsData(res.data);
            }
        })
    }
    //获取职位
    const getPositionsHook = (data?: object) => {
        getAllPositionsApi(data).then(res => {
            if (res.code == 200) {
                setPositionsData(res.data);
            }
        })
    }

    //获取职级
    const getRanksHook = (data?: object) => {
        getRanksApi(data).then(res => {
            if (res.code == 200) {
                setRanksData(res.data);
            }
        })
    }
    //部门 
    const getOrganization = async (data?: object) => {
        const res = await getOrganizationApi(data);
        if (res.code == 200) {
            setOrganizationData(res.data)
        }
    }
    //工作地点
    const getWorkPlaceHook = async (data?: object) => {
        const res = await getWorkplacesApi(data);
        if (res.code == 200) {
            setWorkPlaceData(res.data)
        }
    }
    //最新工号
    const getNewestWorkNumberDataHook = async () => {
        const res = await getNewestWorkNumberDataApi();
        if (res.code == 200) {
            setWorkNumberData(res.data)
        }
    }
    return {
        getCountriesHook, countriesData, getCitiesHook, citiesData, getOrganizationTypesHook, organizationTypesData,
        getJobsHook, jobsData, getPositionsHook, positionsData, getRanksHook, ranksData, getWorkPlaceHook, workplaceData,
        getOrganization, organizationData, getNewestWorkNumberDataHook, workNumberData

    }
}

export default useFindData